package com.click369.mianshiqingbao.service.impl;

import com.click369.mianshiqingbao.mapper.StoreBeanMapper;
import com.click369.mianshiqingbao.pojo.StoreBean;
import com.click369.mianshiqingbao.pojo.StoreBeanExample;
import com.click369.mianshiqingbao.pojo.StoreBeanExample.Criteria;
import com.click369.mianshiqingbao.pojo.entity.PageResult;
import com.click369.mianshiqingbao.pojo.entity.Result;
import com.click369.mianshiqingbao.service.StoreService;
import com.click369.mianshiqingbao.util.StaticFinalFile;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户收藏信息的业务访问接口实现类
 */
@Service
public class StoreServiceImpl implements StoreService{
    @Autowired
    private StoreBeanMapper storeBeanMapper;
    /**
     * 条件分页查询用户收藏信息列表
     * @return 分页查询信息的封装类对象
     */
    @Override
    public PageResult findPage(StoreBean storebean, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<StoreBean> page=(Page<StoreBean>)storeBeanMapper.selectByExample(null);
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 增加用户收藏信息
     * @param storebean 用户收藏信息对象
     */
    @Override
    public void add(StoreBean storebean) {
        storeBeanMapper.insert(storebean);
    }
    /**
     * 根据ID获取用户收藏信息对象
     * @param id 用户收藏信息的id
     * @return 用户收藏信息对象
     */
    @Override
    public StoreBean findOne(Long id) {
        return storeBeanMapper.selectByPrimaryKey(id);
    }
    /**
     * 修改用户收藏信息
     * @param storebean 用户收藏信息对象
     */
    @Override
    public void update(StoreBean storebean) {
        storeBeanMapper.updateByPrimaryKey(storebean);
    }
    /**
     * 批量删除用户收藏信息
     * @param ids 批量删除用户收藏信息的id集合
     */
    @Override
    public void delete(Long[] ids) {
        for(long id:ids){
            storeBeanMapper.deleteByPrimaryKey(id);
        }
    }
    /**
     *根据当前用户id，题目id删除收藏
     *  @param  userId
     * @param topicId 题目id
     * @return 成功或失败的封装类
     */
    @Override
    public StoreBean getCollectByTopicId(Long userId, Long topicId) {
        StoreBeanExample storeBeanExample=new StoreBeanExample();
        StoreBeanExample.Criteria criteria= storeBeanExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andTopicIdEqualTo(topicId);
        List<StoreBean> storeBeanList= storeBeanMapper.selectByExample(storeBeanExample);
        if (storeBeanList.size()>0){
            return storeBeanList.get(0);
        }
        return null;
    }


}
